/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.collections.Map;

import fever.display.cursor.Cursor;

/**
 * Simple cursors manager.
 * 
 * <p>{@link fever.display.Cursor} must be registred using {@link #add} method 
 * before usgin them on manager.
 * 
 * <p>Good practise is to build a Cursor ID enumeration list to keep correct 
 * cursor's ID registration name.
 * 
 * <p>Example
 * {@code
 *   class CursorList
 *   {
 *     public static var BUSY_CURSOR : String = 'Busy';
 *     
 *     
 *     private function CursorList() { }
 *   }
 * }
 * 
 * <p>Example
 * {@code
 *   CursorManager.showCursor( 
 *     CursorManager.add( new FlexBusyCursor(), CursorList.BUSY_CURSOR ) 
 *   );
 * }
 * 
 * @author Romain Ecarnot
 */
class fever.display.cursor.CursorManager
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _cursorMap : Map = new Map();
	private static var _sCurrentCursorName : String;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Registers passed-in {@code cursor} in manager.
	 * 
	 * <p>Cursor must be registred before used in manager.
	 * 
	 * @param cursor {@link fever.display.Cursor} instance
	 * @param cursorName Cursor registration ID
	 */
	public static function add( cursor : Cursor, cursorName : String ) : String
	{
		if( cursorName && cursor )
		{
			_cursorMap.put( cursorName, cursor );
			return cursorName;
		}
		
		return null;
	}
	
	/**
	 * Removes cursor registred with passed-in {@code cursorName} 
	 * from manager.
	 * 
	 * <p>Cursor can never being displayed after removing, use 
	 * {@link #add} method to register it again.
	 */
	public static function remove( cursorName : String ) : Void
	{
		_cursorMap.remove( cursorName );
	}
	
	/**
	 * Returns {link fever.display.Cursor} instance registred 
	 * with {@code cursorName} id.
	 */
	public static function get( cursorName : String ) : Cursor
	{
		return Cursor( _cursorMap.get( cursorName ) );
	}
	
	/**
	 * Returns {@code true} if {@code cursorName} is registred in 
	 * manager.
	 */
	public static function isRegistred( cursorName : String ) : Boolean
	{
		return _cursorMap.containsKey( cursorName );
	}
	
	/**
	 * Shows cursor registred with {@code cursorName} ID.
	 */
	public static function showCursor( cursorName : String ) : Void
	{
		hideCursor();
		
		if( CursorManager.isRegistred( cursorName ) )
		{
			var cursor : Cursor = CursorManager.get( cursorName );
			cursor.show();	
			
			_sCurrentCursorName = cursorName;
		}
	}
	
	/**
	 * Hides current cursor.
	 */
	public static function hideCursor( ) : Void
	{
		if( _sCurrentCursorName )
		{
			var cursor : Cursor = CursorManager.get( _sCurrentCursorName );
			cursor.hide();
			
			_sCurrentCursorName = null;
		}
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function CursorManager() { }
	
	
}